package com.yinhai.message.implement.im.sender;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jgroups.util.UUID;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.yinhai.im.api.client.YinHaiIMAPI;
import com.yinhai.message.api.bean.TMMessage;
import com.yinhai.message.api.bean.TMUser;
import com.yinhai.message.api.exception.TMException;
import com.yinhai.message.api.vo.TMMessageVo;
import com.yinhai.message.core.sender.IMessageSender;
import com.yinhai.message.core.util.DateFormatUtil;
import com.yinhai.message.core.util.IMessageConstant;
import com.yinhai.message.implement.im.tool.ImApiManager;


/**
 * Created by zhou on 2017/3/29.
 */
@Service
public class ImMessageSender implements IMessageSender<TMMessageVo> {
   
    @Override
    public List<TMUser> sendMessage(TMMessageVo messageSendVO){
        TMMessage tmmsg = messageSendVO.getMessage();
        TMUser sendUser = messageSendVO.getUser();

        //im通知主题
//        String title = tmmsg.getTitle();
        //im通知内容
//        String content = tmmsg.getContent();
        //发送人
        String senderID = sendUser.getUserId();
        //接收人
        List<TMUser> receives = messageSendVO.getUserlist();
        List<TMUser> resultUserList = new ArrayList<>();
        
        YinHaiIMAPI api = ImApiManager.getApi();
        //发送
        StringBuilder ids = new StringBuilder();
		for (TMUser receive : receives) {
			TMUser exuser = new TMUser();
			exuser.setAppId(receive.getAppId());
			exuser.setUserId(receive.getUserId());
			exuser.setSendSign(IMessageConstant.MSG_STATE_SEND_ALREADY);
//			try {
//				String result = api.sendNotify(senderID, receive.getImId(), title, content, "10000");
//				if(!"0".equals(result)){
//					exuser.setSendSign(IMessageConstant.MSG_STATE_SEND_FAIL);
//					exuser.setErrorInfo(result);
//				}
//			} catch (Exception e) {
//				e.printStackTrace();
//				exuser.setSendSign(IMessageConstant.MSG_STATE_SEND_FAIL);
//				exuser.setErrorInfo(e.getMessage());
//			}
			ids.append(receive.getUserId()).append(";");
			resultUserList.add(exuser);
		}

		String receivedids = ids.substring(0, ids.length()-1);
		Map<String, String> msg = new HashMap<>();
		msg.put("mgid", tmmsg.getMgid());
		msg.put("title", tmmsg.getTitle());
		msg.put("content", tmmsg.getContent());
		msg.put("mgdateShow", DateFormatUtil.df2.format(tmmsg.getCreateDate()));
		msg.put("suserid", sendUser.getUserId());
		msg.put("sname", sendUser.getName());
		msg.put("read", "0");
		msg.put("selfType", tmmsg.getSelfType());
		msg.put("otherData1", tmmsg.getOtherData1());
		msg.put("otherData2", tmmsg.getOtherData2());
		msg.put("otherData3", tmmsg.getOtherData3());
		try {
			String result = api.sendMessage(senderID, receivedids, JSON.toJSONString(msg), UUID.randomUUID().toString());
			if(!"0".equals(result)){
				throw new TMException("调用im接口返回结果异常("+result+")");
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new TMException(e);
		}
        return resultUserList;
    }
}
